<?php
/**
 * Class mapController
 * desc this class base amap REST api
 */
class mapController extends commonController
{
	static protected $visitpath='';//图片访问路径
	static protected $filepath ="";
    static protected $key = '';
    static protected $key1 = '';
    static protected $key2 = '';
    static protected $key3 = '';
    static protected $key4 = '';

	private $url = "http://restapi.amap.com/v3/";
	public function __construct()
	{
		parent::__construct();
		$this->visitpath=config('URL_HTTP_PROTOCOL').$_SERVER['HTTP_HOST']."/upload/";
		$this->filepath=ROOT_PATH."/upload/";
		$this->key0 = 'c14dcd515e589f4f22cad8bac53cbee9';
        $this->key1 = '692dc3ca03a94b49fe4e3bdadcbfcb4b';
        $this->key2 = '2b58024369cd78eafc866d1559baece2';
        $this->key3 = '7a6c7a5ca2cd29081ba0e25edb306684';
        $this->key4 = '7a7aa11bff29a38e2953f81d1e30eb05';
        $this->key5 = 'cd93b1ed3a9447e42a37693c15a990b1';
        $this->key6 = 'e53132a6786bf70d2fe25dcb6e16b0df';
        $this->key7 = 'da9aef7a0f50ba23de760e0b3d2a12f5';
        $this->key8 = 'dc3fd53868629c7c781b6ae8c82ca43c';
        $this->key9 = 'c60f758a928a63ea069118e7942e9680';
        $this->key10 = 'c1d92c382d29e9cad256d6d85be60f5e';
        $this->key11 = '089c20f6f789b5b853b09d09946ec6ae';
        $this->key12 = 'fbefa62c34cec294212fc1bd21fbf597';
        $this->key13 = '6c443e44effdd75a06f9a0efed8a310b';
        $this->key14 = 'ec19234a8947aa3b07ecb20435c53e9b';


	}
	//地理编码查询
    //老assistant/inputtips?key=$this->key&keywords=$keywords
    //新http://restapi.amap.com/v3/geocode/geo?key=$this->key&address=$keywords
    protected function geocode($keywords,$city = null,$key = 0){
        $key = 'key'.$key;
        echo 'key：',$key,'----《';
        if($city ==null) {
            $url = "restapi.amap.com/v3/geocode/geo?key=".$this->$key."&address=$keywords";
        }else{
            $url = "restapi.amap.com/v3/geocode/geo?key=".$this->$key."&address=$keywords&city=$city";
        }

        return $this->curl($url, '', '', "GET");
    }


	//循环法
	public function getlocation($address,$city=null){
	    echo $address,'<------------>';
        for($i = 0;$i<15;$i++){
            $info = $this->geocode($address,$city,$i);
            if($info==false||$info ==null||$info == ''){
                continue;
            }
            $json = json_decode($info,true);
            //print_r($json);die;
            $data = array();
            if($json['status']==0){
                continue;
            }
            if($json['infocode'] == '10000') {
                $locationstr = $json['geocodes'][0]['location'];
                $location = explode(',',$locationstr);
                $data['longitude'] = $location[0];
                $data['latitude'] = $location[1];
                $data['city'] = $json['geocodes'][0]['city'];
                if($data['city'] == ''|| empty($data['city'])){
                    $data['city'] = '暂无';
                }
                return $data;
            }elseif($json['infocode']=='10004'){
                sleep(61);
                continue;
            }elseif ($json['infocode'] == '10003'){
                cpError::write('超出日流量:'.$json['infocode'].'Thekey:'.$i,"获取坐标错误");
                continue;
            }else {
                cpError::write('错误代码:'.$json['infocode'].'Thekey:'.$i,"获取坐标错误");
                continue;
            }
        }

        return false;
    }



//递归法
/*	public function getlocationdigui($address,$city=null,$fun = 'geocode',$deep = 0){
	    if($deep == 5){
	        return false;
        }
        $fun = $fun.$deep;
        $info = $this->$fun($address,$city);
        $json = json_decode($info,true);
        $data = array();
        if(!empty($json)){
            if($json['status']==0){
                return false;
            }
            if($json['infocode'] == '10000'){

                $locationstr = $json['geocodes'][0]['location'];
                $location = explode(',',$locationstr);
                $data['longitude'] = $location[0];
                $data['latitude'] = $location[1];

                return $data;
            }elseif($json['infocode']=='10004'){
                sleep(61);
                return $this->getlocation($address,$city,$fun,$deep);
            }elseif ($json['infocode'] == '10003'){
                return $this->getlocation($address,$city,'geocode',$deep+1);
            }else {
                $thekey = 'key'.$deep;
                cpError::write('错误代码:'.$json['infocode'].'Thekey:'.$this->$thekey,"获取坐标错误");
                return $this->getlocation($address,$city,$fun,$deep);
            }
        }
        return false;
    }*/


  /*  protected function geocode0($keywords,$city = null){
        if($city ==null) {
            $url = "restapi.amap.com/v3/geocode/geo?key=$this->key0&address=$keywords";
        }else{
            $url = "restapi.amap.com/v3/geocode/geo?key=$this->key0&address=$keywords&city=$city";
        }
        return $this->curl($url, '', '', "GET");
    }
    protected function geocode1($keywords,$city = null){
        if($city == null) {
            $url = "restapi.amap.com/v3/geocode/geo?key=$this->key1&address=$keywords";
        }else{
            $url = "restapi.amap.com/v3/geocode/geo?key=$this->key1&address=$keywords&city=$city";
        }
        return $this->curl($url, '', '', "GET");
    }
    protected function geocode2($keywords,$city = null){
        if($city == null) {
            $url = "restapi.amap.com/v3/geocode/geo?key=$this->key2&address=$keywords";
        }else{
            $url = "restapi.amap.com/v3/geocode/geo?key=$this->key2&address=$keywords&city=$city";
        }
        return $this->curl($url, '', '', "GET");
    }
    protected function geocode3($keywords,$city = null){
        if($city == null) {
            $url = "restapi.amap.com/v3/geocode/geo?key=$this->key3&address=$keywords";
        }else{
            $url = "restapi.amap.com/v3/geocode/geo?key=$this->key3&address=$keywords&city=$city";
        }
        return $this->curl($url, '', '', "GET");
    }
    protected function geocode4($keywords,$city = null){
        if($city == null) {
            $url = "restapi.amap.com/v3/geocode/geo?key=$this->key4&address=$keywords";
        }else{
            $url = "restapi.amap.com/v3/geocode/geo?key=$this->key4&address=$keywords&city=$city";
        }
        return $this->curl($url, '', '', "GET");
    }*/


    //逆地理编码查询
    public function regeo($location){
        $url = $this->url . "geocode/regeo?&radius=1000&extensions=all&location=$location&key=$this->key";
        return $this->curl($url,'', '', "GET");
    }
    //coordsys 可选值：gps;mapbar;baidu
    public function convert($locations,$coordsys){
        $url = $this->url . "assistant/coordinate/convert?coordsys=$coordsys&output=json&locations=$locations&key=$this->key";
        return $this->curl($url,'', '', "GET");
    }



    /**
	curl
	 */
	private function curl($url, $data, $header = false, $method = "POST")
	{
	    echo $url;
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		if ($header) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		}
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
		if ($data) {
			curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
		}
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$ret = curl_exec($ch);
        curl_close($ch);

        return $ret;
	}
}